using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using Sedna.Core.Controller.Configurations.Enums;
using Sedna.Core.Globalization;

namespace Sedna.Core.Controller.Configurations
{
    public class Enum2StringKeyDataTranslator: IDataTranslator
    {
        private string keyPrefix;
        private string keyMask;
        private IDictionary value2key;
        private IMessageManager messageManager;


        public string KeyPrefix
        {
            set { keyPrefix = value; }
        }

        public string KeyMask
        {
            set { keyMask = value; }
        }

        public IDictionary Value2key
        {
            set { value2key = value; }
        }

        public IMessageManager MessageManager
        {
            set { messageManager = value; }
        }

        public virtual object Translate(Control control, object value)
        {
            return Translate(control, value, null);
        }

        public virtual object Translate(Control control, object value, Type targetType)
        {
            if (!(value is Enum)) return null;
            return messageManager.GetMessage(GetKey((Enum)value));
        }
        
        private string GetKey(Enum value)
        {
            string key = null;
            if (value2key!=null)
            {
                key = (string)value2key[value.ToString()];
            }
            return key!=null?key:String.Format(keyMask, keyPrefix, value.GetType().Name, value.ToString());
        }
    }
}
